package cn.psvmc.cxzapi.controller.market;

import cn.psvmc.cxzapi.model.common.DelEntity;
import cn.psvmc.cxzapi.model.common.MarketEntity;
import cn.psvmc.cxzapi.model.market.TMarket;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.market.TMarketService;
import cn.psvmc.cxzapi.util.ToolUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 营销首页管理
 * @Author auto
 * @Date 2024-08-13
 */
@RestController
@RequestMapping("/market")
public class TMarketController {

    @Resource
    private TMarketService tMarketService;

    /**
    * @Author auto 2024-08-13 新增营销首页表
    * @Param tMarket 营销首页表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TMarket tMarket) {
        ToolUtils.validation(new Object[]{tMarket.getComponentId(), "组件id", 2, tMarket.getComponentType(), "组件类型", 1});
        int addResult = tMarketService.add(tMarket);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
    * @Author auto 2024-08-13 更新营销首页表
    * @Param tMarket 营销首页表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TMarket tMarket) {
        ToolUtils.validation(new Object[]{tMarket.getMarketid(), "id", 1, tMarket.getRversion(), "版本", 1});
        int updateResult = tMarketService.edit(tMarket);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-13 删除营销首页表
    * @Param [entity] 删除实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/delete")
    public Result delete(@RequestBody MarketEntity entity) {
        String componentId = entity.getComponentId();
        ToolUtils.validation(new Object[]{componentId, "组件id", 1});
        tMarketService.delete(componentId);
        return ResultGenerator.genSuccessResult("删除成功！");
    }

    /**
    * @Author auto 2024-08-13 查询营销首页表列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list")
    public Result list(@RequestBody MarketEntity entity) {
        return ResultGenerator.genSuccessResult("查询成功！", tMarketService.listAll(entity));
    }

    /**
     * @Description 排序
     * @Date 2024/8/18 12:39
     * @Author YWX
     * @Param [markets]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/sort")
    public Result sort(@RequestBody List<TMarket> markets) {
        ToolUtils.validation(new Object[]{markets, "组件", 3});
        tMarketService.sort(markets);
        return ResultGenerator.genSuccessResult("更新成功！");
    }
}
